const taskService = require('../service/task.service')
const { currentDate } = require('../utils/time-tool')

class TaskController {

  async fetchTaskList(ctx, next) {
    let { limit, offset, date } = ctx.query
    if (!limit) limit = 10
    if (!offset) offset = 0
    if (!date) date = currentDate()
    const result = await taskService.fetchTaskList(limit, offset, date)

    ctx.body = {
      code: 0,
      message: '获取任务列表成功~',
      data: result
    }
  }

  async updateTask(ctx, next) {
    const { id, staffId, timeIndexs, date } = ctx.request.body
    // 为-1时候，新增
    if (id == -1) {
      const creatorId = ctx.user.id
      const result = await taskService.create({
        staffId,
        creatorId,
        timeIndexs,
        date
      })

      ctx.body = {
        code: 0,
        message: '添加派单信息成功~',
        data: result
      }
    } else {
      const result = await taskService.update({
        id,
        timeIndexs
      })

      ctx.body = {
        code: 0,
        message: '更新派单信息成功~',
        data: result
      }
    }
  }
}

module.exports = new TaskController()